<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style></style>
    <link rel="stylesheet" href="./css/main.css" />
  </head>
  <body>
    <div class="img-box" id="imgBox">
      <div class="img-big">
        <img id="imgBig" src="./imgs/1.jpg" alt="" />
      </div>
      <div class="img-thumbs" id="imgThumbs">
        <div class="img-thumb" style="border-color: yellow">
          <img data-thumb src="./imgs/1.jpg" alt="" />
        </div>
        <div class="img-thumb">
          <img data-thumb src="./imgs/2.jpg" alt="" />
        </div>
        <div class="img-thumb">
          <img data-thumb src="./imgs/3.jpg" alt="" />
        </div>
        <div class="img-thumb">
          <img data-thumb src="./imgs/4.jpg" alt="" />
        </div>
        <div class="img-thumb">
          <img data-thumb src="./imgs/5.jpg" alt="" />
        </div>
        <div class="img-thumb active">
          <img data-thumb src="./imgs/6.jpg" alt="" />
        </div>
        <div class="img-thumb">
          <img data-thumb src="./imgs/7.jpg" alt="" />
        </div>
        <div class="img-thumb">
          <img data-thumb src="./imgs/8.jpg" alt="" />
        </div>
      </div>
    </div>
    <script>
      const imgBox = document.getElementById("imgBox");
      const imgBig = document.getElementById("imgBig");

      imgBox.onclick = function (e) {
        // imgBox.onmouseover = function (e) {
        const thumb = e.target.dataset.thumb;
        if (thumb === undefined) return;
        // 获取缩略图的src图片地址
        const thumbSrc = e.target.src;
        // 替换大图的src
        imgBig.src = thumbSrc;

        // 取消所有的选中高亮
        imgBox
          .querySelectorAll(".img-thumb")
          .forEach((el) => (el.style.borderColor = ""));
        // 获取上级节点 .img-thumb
        const preDom = e.target.closest(".img-thumb");
        // 修改边框颜色
        preDom.style.borderColor = "yellow";
      };
    </script>
  </body>
</html>
